package com.inforich.web.controller.appManage;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.inforich.appManage.vo.AppUserVO;
import java.util.Arrays;
import cn.hutool.core.bean.BeanUtil;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestParam;
import com.inforich.common.annotation.Log;
import com.inforich.common.core.controller.BaseController;
import com.inforich.common.core.domain.AjaxResult;
import com.inforich.common.enums.BusinessType;
import com.inforich.appManage.domain.AppUser;
import com.inforich.appManage.service.IAppUserService;
import com.inforich.common.utils.poi.ExcelUtil;

/**
 * app用户Controller
 *
 * @author hjm
 * @date 2022-10-27
 */
@RestController
@RequestMapping("/appManage/appUser")
public class AppUserController extends BaseController {

  @Autowired
  private IAppUserService appUserService;

  /**
   * 查询app用户列表
   */
  @PreAuthorize("@ss.hasPermi('appManage:appUser:list')")
  @GetMapping("/list")
  public Page<AppUserVO> list(Page<AppUserVO> page, AppUserVO appUser) {
    Map<String, Object> map = beanToMap(appUser);
    appUserService.pageAppUserVO(page, map);
    return page;
  }


  /**
   * 导出app用户列表
   */
  @PreAuthorize("@ss.hasPermi('appManage:appUser:export')")
  @Log(title = "app用户", businessType = BusinessType.EXPORT)
  @PostMapping("/export")
  public void export(HttpServletResponse response, AppUserVO appUser) {
    Map<String, Object> map = beanToMap(appUser);
    List<AppUserVO> list = appUserService.listAppUserVO(map);
    ExcelUtil<AppUserVO> util = new ExcelUtil<AppUserVO>(AppUserVO.class);
    util.exportExcel(response, list, "app用户数据");
  }

  /**
   * 获取app用户详细信息
   */
  @PreAuthorize("@ss.hasPermi('appManage:appUser:query')")
  @GetMapping(value = "/{id}")
  public AjaxResult getInfo(@PathVariable("id") String id) {
    return AjaxResult.success(appUserService.getById(id));
  }

  /**
   * 新增app用户
   */
  @PreAuthorize("@ss.hasPermi('appManage:appUser:add')")
  @Log(title = "app用户", businessType = BusinessType.INSERT)
  @PostMapping
  public AjaxResult add(@Validated @RequestBody AppUser appUser) {
    return toAjax(appUserService.save(appUser));
  }

  /**
   * 修改app用户
   */
  @PreAuthorize("@ss.hasPermi('appManage:appUser:edit')")
  @Log(title = "app用户", businessType = BusinessType.UPDATE)
  @PutMapping
  public AjaxResult edit(@Validated @RequestBody AppUser appUser) {
    return toAjax(appUserService.updateById(appUser));
  }

  /**
   * 删除app用户
   */
  @PreAuthorize("@ss.hasPermi('appManage:appUser:remove')")
  @Log(title = "app用户", businessType = BusinessType.DELETE)
  @DeleteMapping("/{id}")
  public AjaxResult remove(@PathVariable String id) {
    return appUserService.removeAppUser(id);
  }

  /**
   * 启用禁用
   */
  @PreAuthorize("@ss.hasPermi('appManage:appUser:disableEnable')")
  @PostMapping("/disableEnable")
  public AjaxResult disableEnable(@RequestBody AppUser appUser) {
    return AjaxResult.success(appUserService.update(new LambdaUpdateWrapper<AppUser>()
        .set(AppUser::getStatus, appUser.getStatus())
        .eq(AppUser::getId, appUser.getId())));
  }
}
